<?php

namespace app\rental\controller;

use app\rental\model\RentalBrandModel;
use app\rental\model\RentalCarModel;
use app\rental\model\RentalCategoryModel;
use app\rental\model\RentalEmissionModel;
use app\rental\model\RentalPriceRangeModel;
use app\rental\model\RentalRankModel;
use app\rental\model\RentalYearModel;
use cmf\controller\HomeBaseController;

class ListController extends HomeBaseController
{
    public function _initialize()
    {
        parent::_initialize();
        define('MODULE_NAME', request()->module()); //模块名称
        define('CONTROLLER_NAME', request()->controller()); //控制器名称
        define('ACTION_NAME', request()->action());
    }
    public function index()
    {
        $rentalBrandModel = new RentalBrandModel();
        $rentalCategoryModel = new RentalCategoryModel();
        $rentalPriceRangeModel = new RentalPriceRangeModel();
        $rentalRankModel = new RentalRankModel();
        $rentalYearModel = new RentalYearModel();
        $rentalCarModel = new RentalCarModel();
        $rentalEmissionModel = new RentalEmissionModel();
        $where['delete_time'] = 0;
        $limit = 16;
        $param_brand = request()->param('brand');
        if (!empty($param_brand)) {
            $where['brand_id'] = $param_brand;
            $this->assign("brand", $rentalBrandModel->where('id', $where['brand_id'])->find());
        }
        $param_price_range = request()->param('price_range');
        if (!empty($param_price_range)) {
            $where['price_range_id'] = $param_price_range;
            $this->assign("price_range", $rentalPriceRangeModel->where('id', $where['price_range_id'])->find());
        }
        $param_rank = request()->param('rank');
        if (!empty($param_rank)) {
            $where['rank_id'] = $param_rank;
            $this->assign("rank", $rentalRankModel->where('id', $where['rank_id'])->find());
        }
        $param_emission = request()->param('emission');
        if (!empty($param_emission)) {
            $where['emission_id'] = $param_emission;
            $this->assign("emission", $rentalEmissionModel->where('id', $where['emission_id'])->find());
        }
        $yearParam = request()->param('year');
        if (isset($yearParam)) {
            $where['year_id'] = $yearParam;
            $this->assign("year", $rentalYearModel->where('id', $yearParam)->find());
            if (intval($yearParam) == 0) {
                $this->assign("year", ['id' => 0, 'year' => '新车']);
            }
        }
        $param_id = request()->param('id');
        if (!empty($param_id)) {
            $where['category_id'] = $param_id;
        }

        $id = $this->request->param('id', 0, 'intval');
        $category = $rentalCategoryModel->where('id', $id)->where('status', 1)->find();
        $this->assign('category', $category);
        $this->assign("categorys", $rentalCategoryModel->getIndexCategorys());
        $rentalCategory = $rentalCategoryModel->getRentalCategory();

        if (!empty($where['category_id']) && $where['category_id'] == $rentalCategory) {
            // $rental_brands = $rentalCarModel->where(['category_id'=>$rentalCategory,'post_status'=>1])->column('brand_id');
            $rental_brands = $rentalBrandModel->column('id');
            $limit = 9;
            $this->assign("rental_brands", array_unique($rental_brands));
        }
        $this->assign("rental_category", $rentalCategory);
        $this->assign("brands", $rentalBrandModel->getBrands());
        $this->assign("price_ranges", $rentalPriceRangeModel->getPriceRange());
        $this->assign("ranks", $rentalRankModel->getRank());
        $this->assign("years", $rentalYearModel->getYears());
        $this->assign("emissions", $rentalEmissionModel->where(['status' => 1])->field('id,emission')->select()->toArray());
        $cars = $rentalCarModel->where($where)->order("list_order desc")->paginate($limit);

        $this->assign("cars", $cars);
        $this->assign('page', $cars->render());
        $listTpl = empty($category['list_tpl']) ? 'list' : $category['list_tpl'];
        return $this->fetch('/' . $listTpl);
    }

}
